package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;
    /**
     * 查找所有检查组
     * @return
     */
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }

    /**
     * 跟据id查找检查组
     * @param id
     * @return
     */
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findeById(id);
    }

    /**
     * 根据id删除检查组
     * @param id
     * @return
     */
    public boolean deleteById(Integer id) {
        return checkGroupMapper.deleteById(id);
    }
    /**
     * 根据套餐id查询关联的检查项id
     * @param setmealId
     * @return
     */
    public List<Integer> findCheckGroupIdsBySetmealId(Integer setmealId) {
        return checkGroupMapper.findCheckGroupIdsBySetmealId(setmealId);
    }

    /**
     * 编辑
     * @param checkGroup
     * @return
     */
    public boolean edit(CheckGroup checkGroup,List<Integer> ids) {
        if (ids.size()<0){
            throw new RuntimeException(MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
        for (int i :ids){
            checkGroupMapper.editGroup(i,checkGroup.getId());
        }
        return checkGroupMapper.edit(checkGroup);
    }

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<CheckGroup> page = (Page<CheckGroup>) checkGroupMapper.findAll();
        return new  PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增检查组并加入对于的检查项
     * @param checkGroup
     * @return
     */
    public boolean add(CheckGroup checkGroup , List<Integer> checkitemIds) {

        if (checkGroupMapper.add(checkGroup)){
            for(int i:checkitemIds){
                checkGroupMapper.CheckGroupInItem(checkGroup.getId(),i);
            }

        }
        return true;
    }

}
